package com.diyunapp.xilinapp.me;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.bean.XilinCoinBean;
import com.diyunapp.xilinapp.main.MainActivity;
import com.diyunapp.xilinapp.selfdefine.StatusBarFragmentActivity;
import com.diyunapp.xilinapp.utils.AbDateUtil;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class XiLinCoinActivity extends StatusBarFragmentActivity {

    @Bind(R.id.recharge_record_tv)
    TextView rechargeRecordTv;
    @Bind(R.id.go_back)
    ImageView goBack;
    @Bind(R.id.account_money)
    TextView accountMoney;
    @Bind(R.id.integral_fresh_lv)
    ListView integralFreshLv;
    @Bind(R.id.integral_refresh_view)
    SwipeRefreshLayout integralRefreshView;
    private List<XilinCoinBean.DateBean> info=new ArrayList<>();
    private XiLinCoinAdapter xiLinCoinAdapter;
//    private List integrals;
    private LayoutInflater inflater;
    private String xiLinCoinNum;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xi_lin_coin);
        inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        xiLinCoinAdapter=new XiLinCoinAdapter(this);
        ButterKnife.bind(this);
        View headView;
        headView = findViewById(R.id.xilibcoin_menubar_inc);
        super.MenuWidget(headView);
        xiLinCoinNum=getIntent().getExtras().getString("member_xlb");
        accountMoney.setText(xiLinCoinNum);
        getData();
        integralFreshLv.setAdapter(xiLinCoinAdapter);
        integralRefreshView.setColorSchemeColors(R.color.domain_orange, R.color.green,
                R.color.blue, R.color.yellow);

        integralRefreshView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Log.d("zhangxiaoxidebug", "myordersSwiperefreshlayout onRefresh...");
                getData();
            }
        });
    }

    private void getData() {
        String url = ""+MainActivity.url+"act=goods&op=my_xlb&username="+ MainActivity.myLogin.getUsername()+"";

        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);
        http.send(HttpRequest.HttpMethod.POST, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {}
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        integralRefreshView.setRefreshing(false);
//                        bidFreshLv.onRefreshComplete();
                        String resultinfo = responseInfo.result;
                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
//                        Logger.e("=================================="+resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.optInt("code");
                            if (result == 200) {
                                info.clear();
                                JSONArray listArray = resultObject.optJSONArray("date");
                                for (int i = 0; i < listArray.length(); i++) {
                                    JSONObject recordObject = listArray.optJSONObject(i);

                                    long add_time = recordObject.optLong("add_time");
//                                    toast(String.valueOf(add_time));
                                    String op_desc = recordObject.optString("op_desc");
                                    String xlb = recordObject.optString("xlb");
                                    Log.i("===================", "op_desc=" + op_desc);
                                    Log.i("===================", "add_time=" + add_time);
                                    Log.i("===================", "xlb=" + xlb);

//                                    Logger.e("登录接口完成！");
//                                    Logger.e("=================================="+add_time);
//                                    Logger.e("=================================="+op_desc);
//                                    Logger.e("=================================="+xlb);
                                    info.add(new XilinCoinBean.DateBean(add_time, op_desc, xlb));

                                }
                                xiLinCoinAdapter.setList(info);
                                xiLinCoinAdapter.notifyDataSetChanged();

                            } else {
                                toast("当前暂无数据");
                                integralRefreshView.setVisibility(View.GONE);

                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            toast("查询失败");
                            e.printStackTrace();
                            integralRefreshView.setVisibility(View.GONE);
                        }
                    }
                    @Override
                    public void onFailure(HttpException error, String msg) {
                        integralRefreshView.setRefreshing(false);
                        toast("请求失败");
                        integralRefreshView.setVisibility(View.GONE);
                    }
                });
    }
    @OnClick({R.id.go_back,R.id.recharge_record_tv})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.go_back:
                finish();
                break;
            case R.id.recharge_record_tv:

                break;
        }
    }


    class XiLinCoinAdapter extends BaseAdapter {
        private List<XilinCoinBean.DateBean> info = new ArrayList<XilinCoinBean.DateBean>();
        private Context context;
        public XiLinCoinAdapter(Context context) {
            super();
            this.context = context;
        }

        public void setList(List<XilinCoinBean.DateBean> list) {
            this.info = list;
            notifyDataSetChanged();
        }
        @Override
        public int getCount() {
            return null == info ? 0 : info.size();
        }

        @Override
        public Object getItem(int position) {
            return null == info ? null : info.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }
        private  ViewHolder viewHolder;

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.item_xilincoin_record, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);

            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            XilinCoinBean.DateBean data_info = info.get(position);
            viewHolder.tradeTime.setText(AbDateUtil.getStringByFormat(data_info.getAdd_time()*1000,"yyyy-MM-dd hh:mm:ss"));
            viewHolder.tradeWay.setText(data_info.getOp_desc());
            viewHolder.tradeMoney.setText(data_info.getXlb());
            return convertView;
        }

        class ViewHolder {
            @Bind(R.id.trade_way)
            TextView tradeWay;
            @Bind(R.id.trade_time)
            TextView tradeTime;
            @Bind(R.id.trade_leixing)
            TextView tradeLeixing;
            @Bind(R.id.trade_money)
            TextView tradeMoney;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }
}
